package com.ruoyi.project.cms.service.impl;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.cms.mapper.DivisionLevel5Mapper;
import com.ruoyi.project.cms.domain.DivisionLevel5;
import com.ruoyi.project.cms.service.IDivisionLevel5Service;

/**
 * 行政区划Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-03
 */
@Service
public class DivisionLevel5ServiceImpl extends ServiceImpl<DivisionLevel5Mapper,DivisionLevel5> implements IDivisionLevel5Service    {
    @Autowired
    private DivisionLevel5Mapper divisionLevel5Mapper;

    /**
     * 查询行政区划
     *
     * @param provinceName 行政区划主键
     * @return 行政区划
     */
    @Override
    public DivisionLevel5 selectDivisionLevel5ByProvinceName(String provinceName) {
        return divisionLevel5Mapper.selectDivisionLevel5ByProvinceName(provinceName);
    }

    /**
     * 查询行政区划列表
     *
     * @param divisionLevel5 行政区划
     * @return 行政区划
     */
    @Override
    public Page<DivisionLevel5> selectDivisionLevel5List(DivisionLevel5 divisionLevel5) {
        /*
          <where>
            <if test="provinceName != null  and provinceName != ''"> and province_name like concat('%', #{provinceName}, '%')</if>
            <if test="cityName != null  and cityName != ''"> and city_name like concat('%', #{cityName}, '%')</if>
            <if test="areaName != null  and areaName != ''"> and area_name like concat('%', #{areaName}, '%')</if>
            <if test="streetName != null  and streetName != ''"> and street_name like concat('%', #{streetName}, '%')</if>
            <if test="villageName != null  and villageName != ''"> and village_name like concat('%', #{villageName}, '%')</if>
            <if test="code != null  and code != ''"> and code like concat('%', #{code}, '%')</if>
        </where>
         */
        //pageNum=1&pageSize=10
        //System.out.println(divisionLevel5);
        System.out.println(divisionLevel5.getParams());

        int pageNum = (int) divisionLevel5.getParams().getOrDefault("pageNum", 1);
        int pageSize = (int) divisionLevel5.getParams().getOrDefault("pageSize", 10);
        System.out.println("pageNum"+pageNum);
        System.out.println("pageSize"+pageSize);
        Page<DivisionLevel5> page = new Page<>(pageNum,pageSize);
        //QueryWrapper<DivisionLevel5> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<DivisionLevel5> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.select(DivisionLevel5::getProvinceName,
                DivisionLevel5::getCityName,
                DivisionLevel5::getAreaName,
                DivisionLevel5::getStreetName,
                DivisionLevel5::getVillageName,
                DivisionLevel5::getCode);
        queryWrapper.like(StrUtil.isNotEmpty(divisionLevel5.getProvinceName()),
                DivisionLevel5::getProvinceName,divisionLevel5.getProvinceName());

        queryWrapper.like(StrUtil.isNotEmpty(divisionLevel5.getCityName()),
                DivisionLevel5::getCityName,divisionLevel5.getCityName());

        queryWrapper.like(StrUtil.isNotEmpty(divisionLevel5.getAreaName()),
                DivisionLevel5::getAreaName,divisionLevel5.getAreaName());

        queryWrapper.like(StrUtil.isNotEmpty(divisionLevel5.getStreetName()),
                DivisionLevel5::getStreetName,divisionLevel5.getStreetName());

        queryWrapper.like(StrUtil.isNotEmpty(divisionLevel5.getVillageName()),
                DivisionLevel5::getVillageName,divisionLevel5.getVillageName());

        queryWrapper.like(StrUtil.isNotEmpty(divisionLevel5.getCode()),
                DivisionLevel5::getCode,divisionLevel5.getCode());

       // queryWrapper.last("limit 100");
        return divisionLevel5Mapper.selectPage(page,queryWrapper);
        //return divisionLevel5Mapper.selectDivisionLevel5List(divisionLevel5);
    }

    /**
     * 新增行政区划
     *
     * @param divisionLevel5 行政区划
     * @return 结果
     */
    @Override
    public int insertDivisionLevel5(DivisionLevel5 divisionLevel5) {
        return divisionLevel5Mapper.insertDivisionLevel5(divisionLevel5);
    }

    /**
     * 修改行政区划
     *
     * @param divisionLevel5 行政区划
     * @return 结果
     */
    @Override
    public int updateDivisionLevel5(DivisionLevel5 divisionLevel5) {
        return divisionLevel5Mapper.updateDivisionLevel5(divisionLevel5);
    }

    /**
     * 批量删除行政区划
     *
     * @param provinceNames 需要删除的行政区划主键
     * @return 结果
     */
    @Override
    public int deleteDivisionLevel5ByProvinceNames(String[] provinceNames) {
        return divisionLevel5Mapper.deleteDivisionLevel5ByProvinceNames(provinceNames);
    }

    /**
     * 删除行政区划信息
     *
     * @param provinceName 行政区划主键
     * @return 结果
     */
    @Override
    public int deleteDivisionLevel5ByProvinceName(String provinceName) {
        return divisionLevel5Mapper.deleteDivisionLevel5ByProvinceName(provinceName);
    }
}
